import Vue from 'vue'
import Router from 'vue-router'
import Layout from '../layout/Index.vue'
Vue.use(Router);
//获取原型对象上的push函数
const originalPush = Router.prototype.push
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
export const homeRoutes = [
    {
        path:'/redirect',
        component:Layout,
        hidden:true,
        children: [{
            path:'/redirect/:path(.*)',
            component:()=>import('../view/redirect/index'),
            meta: {title: '重定向动态组件'}
        }]
    },
    {
        path:'/login',
        component:()=>import('../view/login/Index'),
        hidden:true,
        meta: { title: '后台登录' }
    },
    {
        path:'/',
        component:Layout,
        redirect:"/dashboard",
        children:[
            {
                path:'dashboard',
                component:() => import('../view/dashboard/index'),
                name:'dashboard',
                meta: { title: '首页', icon: 'dashboard', affix: true }
            }
        ]
    },

]
export const asyncRoutes = [
    {
        path: '/product',
        component: Layout,
        redirect:'/product/list',
        meta: { title: '产品设置', icon: 'el-icon-guide'},
        children: [
            {
                path: '/product/list',
                component:()=>import('../view/product/Index'),
                meta: { title: '产品列表', icon: 'edit' }
            },
            {
                path: '/product/edit',
                component:()=>import('../view/product/Edit'),
                meta: { title: '产品添加', icon: 'edit' }
            },
            {
                path: '/product/add',
                component:()=>import('../view/product/Add'),
                meta: { title: '产品修改', icon: 'edit' }
            }

        ]
    },
    {
        path: '/member',
        component: Layout,
        redirect:'/member/list',
        meta: { title: '用户设置', icon: 'el-icon-guide'},
        children: [
            {
                path: '/member/list',
                component:()=>import('../view/user/Index'),
                meta: { title: '用户列表', icon: 'edit' }
            },
            {
                path: '/member/edit',
                component:()=>import('../view/user/Edit'),
                meta: { title: '用户添加', icon: 'edit' }
            },
            {
                path: '/member/add',
                component:()=>import('../view/user/Add'),
                meta: { title: '用户修改', icon: 'edit' }
            }

        ]
    },
    {
        path: '/order',
        component: Layout,
        redirect:'/member/list',
        meta: { title: '订单管理', icon: 'el-icon-guide'},
        children: [
            {
                path: '/order/list',
                component:()=>import('../view/order/Index'),
                meta: { title: '订单列表', icon: 'edit' }
            }

        ]
    },
    //项目所用知识DEMO
    {
        path: '/demo',
        component: Layout,
        redirect: '/demo/context',
        children: [
            {
                path: '/demo/context',
                component: ()=>import('../view/Demo/Context'),
                name:'context',
                meta: {title: 'context测试',icon: "dashboard"}
            }
        ]
    }
]

const createRouter = () => new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: homeRoutes
})

const router = createRouter()

//清空路由，把动态加入的路由清空，注入默认路由
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router
